/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.actions;

import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import org.opu.vcforms.bean.db.ConfigPath;
import org.opu.vcforms.editor.actions.MessagesI18n;
import org.opu.vcforms.editor.actions.MessagesI18nImpl;
import org.opu.vcforms.resource.ResourceHandle;
import org.opu.vcforms.resource.ResourceHandle.IconType;
import org.opu.vcforms.view.component.ButtonProperties;
import org.opu.vcforms.view.component.GUIProperties;

/**
 *
 * @author yura
 */
public abstract class AbstractAction extends javax.swing.AbstractAction{
    public static final String ACTIONLABELS = "org.opu.vcforms.actions.ActionLabels";
    protected ResourceBundle boundle;

    protected MessagesI18n i18n;

    public AbstractAction() {
        putValue(NAME, getName());
        this.i18n = new MessagesI18nImpl();
        reset();
    }

    public final void reset(){
        setIcon();
        setText(ConfigPath.getInstance().getLocale());
    }

    protected void setIcon() {
        IconType small = ResourceHandle.IconType.small;
        IconType large = getButtonProp().getIconType();
        putValue(SMALL_ICON, ResourceHandle.getIcon(getIconName(), small));
        putValue(LARGE_ICON_KEY, ResourceHandle.getIcon(getIconName(), large));
    }

    protected ButtonProperties getButtonProp() {
        return GUIProperties.instance().toolbar.button;
    }

    protected void setText(Locale locale) {
        //Locale locale = ConfigPath.getInstance().getLocale();
        boundle = ResourceBundle.getBundle( ACTIONLABELS, locale);
        String n = NAME;
        String sh = SHORT_DESCRIPTION;
        String l = LONG_DESCRIPTION;
        putValue(n, boundle.getString(getName() + "." + n));
        putValue(sh, boundle.getString(getName() + "." + sh));
        putValue(l, boundle.getString(getName() + "." + l));
    }

    public abstract String getName();

    public String getIconName(){
        return getName() + ".png";
    }

    public void errorMsg(Exception ex){
        JOptionPane.showMessageDialog(null,
                i18n.errorMsg() + " \"" + getName() + "\":\n  "
                + ex.getClass().getCanonicalName() + ":\n  " + ex.getMessage(),
                ResourceHandle.getProperty(ResourceHandle.APPLICATION_TITLE),
                JOptionPane.ERROR_MESSAGE);
    }

    public void setI18n(MessagesI18n i18n) {
        this.i18n = i18n;
    }

    public MessagesI18n getI18n() {
        return i18n;
    }
}
